using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WXB.RenderNode
{
	public struct sInitTextData
	{
		public string text;
        public Color color;
		public UIFont font;
        public int fontSize;
		public bool bUnderline;
		public bool bGlint;
	}

	public class TextNode : NodeBase
	{
		const string NodeType = "TextNode";

        public override string getType() { return NodeType; }

		public TextNode()
		{
			d_pFont = null;
			d_bUnderline = false;
		}

		public override NodeBase clone()
		{
			TextNode node = new TextNode();
			node.d_text = d_text;
			node.d_pFont = d_pFont;
            node.d_textColor = d_textColor;
			node.d_bUnderline = d_bUnderline;
            node.d_fontSize = d_pFont.defaultSize;

			return node;
		}

		public override void init(object obj)
		{
			sInitTextData linkData = (sInitTextData)(obj);
			d_text = linkData.text;
            d_textColor = linkData.color;
			d_pFont = linkData.font;
			d_bUnderline = linkData.bUnderline;
			d_bGlint = linkData.bGlint;
            d_fontSize = linkData.fontSize;
		}

        public override bool update(float fTime)
		{
			if (d_bGlint)
			{
				d_elapsed += fTime;
				if (d_elapsed >= 0.2f)
				{
					d_alpha += 0.1f;
					if (d_alpha >= 1.0f)
						d_alpha = 0;

                    d_textColor.a = d_alpha;
                    foreach (UIWidget widget in UIWidgetList)
                    {
                        UILabel label = widget as UILabel;
                        label.color = d_textColor;
                    }

					d_elapsed = 0.0f;
					return true;
				}
			}

			return false;
		}

		protected virtual Color getTextColour()
		{
            return d_textColor;
		}

		protected virtual bool isUnderLine()
		{
			return d_bUnderline;
		}

        public override string getShowText()
        {
            return d_text;
        }

        public override string getAppend()
        {
            return "";
        }

        public override float getHeight()
        {
            if (d_pFont != null)
                return d_pFont.defaultSize;

            return 0;
        }

        public override float getWidth()
        {
            if (d_pFont != null)
            {
                return Fonts.GetTextExtent(d_pFont, d_fontSize, ref d_text);
            }

            return 0;
        }

		protected override void UpdateWidthList()
		{
            d_widthList.Clear();
            if (d_text.Length == 0)
                return;

            d_widthList.Add(Fonts.GetFontWidth(d_pFont, d_fontSize, d_text[0], 0));
			for (int i = 1; i < d_text.Length; ++i)
                d_widthList.Add(Fonts.GetFontWidth(d_pFont, d_fontSize, d_text[i], d_text[i - 1]));
		}

        public virtual bool IsHyText()
        {
            return false;
        }

		public override void render(RenderCache cache, ref float x, ref uint  yline, float zOffset, List<Vector2> vLineSize, Rect targetRect, Rect cliptRect, HorzFormatting xFormatting, float offsetX, float offsetY, float fAlpha)
		{
			if (d_pFont == null)
				return ;

            Vector2 pt = new Vector2(x + offsetX, offsetY);
			for (int i = 0; i < yline; ++i)
				pt.y += vLineSize[i].y;

            float maxWidth = targetRect.width;
			if (maxWidth == 0)
				return ;

			float alignedX = AlignedFormatting(xFormatting, maxWidth, vLineSize[(int)(yline)].x);
			float fHeight  = getHeight();

			pt.y += (vLineSize[(int)(yline)].y - fHeight);

			string tmpText = "";

			Color tmpColour = getTextColour();
            tmpColour.a = fAlpha;
			bool bUnderLine = isUnderLine();
			d_beginLine = yline;

			for (int i = 0; i < d_text.Length; ++i)
			{
                x += d_widthList[i];
                //x += (i == 0 ? Fonts.GetCharExtent(d_pFont, d_fontSize, d_text[i]) : Fonts.GetFontWidth(d_pFont, d_fontSize, d_text[i], d_text[i - 1]));
				if (x > maxWidth)
				{
                    x -= d_widthList[i];
					if (tmpText.Length == 0)
					{
						yline++;
						d_beginLine = yline;
						x = 0.0f;
						
						pt.x = offsetX;
						pt.y = offsetY;
						for (int m = 0; m < yline; ++m)
							pt.y += vLineSize[m].y;

						pt.y  += (vLineSize[(int)(yline)].y - fHeight);
						alignedX = AlignedFormatting(xFormatting, maxWidth, vLineSize[(int)(yline)].x);
						i--;
						continue;
					}

                    Rect area_rect = new Rect(pt.x + alignedX, pt.y, x - pt.x + offsetX, vLineSize[(int)(yline)].y);

                    if (!IsHyText())
                    {
                        cache.cacheText(this, ref tmpText, d_pFont, d_fontSize, area_rect, tmpColour, bUnderLine, false, d_bGlint);
                    }
                    else
                    {
                        string hy = getAppend();
                        cache.cacheHyText(this, ref tmpText, ref hy, d_pFont, d_fontSize, area_rect, tmpColour, bUnderLine, false, d_bGlint);
                    }

					yline++;

					x = 0;
					tmpText = "";

					pt.x = offsetX;
					pt.y = offsetY;
					for (int m = 0; m < yline; ++m)
						pt.y += vLineSize[m].y;

					try
					{
						pt.y += (vLineSize[(int)(yline)].y - fHeight);
					}
					catch (Exception ex)
					{
                        UnityEngine.Debug.LogError(ex.Message);						
					}

					alignedX = AlignedFormatting(xFormatting, maxWidth, vLineSize[(int)(yline)].x);

					i--;
				}
				else
				{
					tmpText += d_text[i];
				}
			}

			if (tmpText.Length != 0)
			{
				Rect area_rect = new Rect(pt.x + alignedX, pt.y, x - pt.x + offsetX, vLineSize[(int)(yline)].y);

                if (!IsHyText())
                {
                    cache.cacheText(this, ref tmpText, d_pFont, d_fontSize, area_rect, tmpColour, bUnderLine, false, d_bGlint);
                }
                else
                {
                    string hy = getAppend();
                    cache.cacheHyText(this, ref tmpText, ref hy, d_pFont, d_fontSize, area_rect, tmpColour, bUnderLine, false, d_bGlint);
                }

                tmpText = "";
			}

			d_endLine = yline;

			if (d_bNewLine == true)
			{
				yline++;
				x = 0;
			}
		}

		protected string d_text;
		protected UIFont d_pFont;
        protected int    d_fontSize;
        protected Color  d_textColor;
		protected bool   d_bUnderline;
	};
}
